import AttributeUtil from '@/AttributeUtil'
import FramePoint from '@/FramePoint'
import { Frame } from '@/frame'
import InputUtil from '@/InputUtil'
import EquipmentQuality from 'code/Scripts/Items/EquipmentQuality'

export default class UICustomItemTipView {
    static isInventory: boolean = false
    constructor() {
        UICustomItemTipView._sl_initTooltip()
        this.initEvent()
    }
    initEvent() {
        for (let i = 0; i < 6; i++) {
            let cmdButton = DzFrameGetItemBarButton(i)
            let cmdButtonFrame = Frame.fromHandle(cmdButton)
            const fi = i
            cmdButtonFrame.addOnMouseEnter(() => {
                UICustomItemTipView.showItemTooltip(fi)
            })
            cmdButtonFrame.addOnMouseLeave(UICustomItemTipView.hideTooltip)
        }

        InputUtil.onMouseMoveEvent(this.mouseMoveEvent)
    }
    mouseMoveEvent() {
        let it = DzGetUnitUnderMouse() as any
        if (it == 0) {
            if (!UICustomItemTipView.isInventory) {
                UICustomItemTipView.hideTooltip()
            }
            return
        }
        if (GetItemTypeId(it) == 0) {
            if (!UICustomItemTipView.isInventory) {
                UICustomItemTipView.hideTooltip()
            }
            return
        }
        UICustomItemTipView.showTipFrames(it, true)
    }
    static showItemTooltip(this: void, i: number) {
        let u = selection()
        if (!IsHandle(u)) {
            return
        }
        UICustomItemTipView.isInventory = true
        let item = UnitItemInSlot(u, i)
        // @ts-ignore
        if (item != 0) {
            UICustomItemTipView.showTooltip(item)
        }
    }
    static showTooltip(item: item) {
        let tooltip = DzFrameGetTooltip()
        DzFrameClearAllPoints(tooltip)
        DzFrameSetAbsolutePoint(tooltip, FramePoint.bottomLeft, 0.81, 0)
        UICustomItemTipView.showTipFrames(item)
    }

    static showTipFrames(item: item, isHover: boolean = false) {
        let itemDB = _g_objs.item[GetItemTypeId(item)]
        if (itemDB == null) return
        let frames = UICustomItemTipView._sl_tooltipFrames
        frames.name.setText(EquipmentQuality.Util.getQualityName(item))
        let labels = UICustomItemTipView.getCostLabelInfo(item)
        // 展示物品信息
        if (labels.length > 0) {
            frames.icon1.setTexture(labels[0].icon)
            frames.label1.setText(labels[0].text)
            frames.icon1.visible = true
            frames.label1.visible = true
        } else {
            frames.icon1.visible = false
            frames.label1.visible = false
        }
        if (labels.length > 1) {
            frames.icon2.setTexture(labels[1].icon)
            frames.label2.setText(labels[1].text)
            frames.icon2.visible = true
            frames.label2.visible = true
        } else {
            frames.icon2.visible = false
            frames.label2.visible = false
        }
        if (labels.length > 2) {
            frames.icon3.setTexture(labels[2].icon)
            frames.label3.setText(labels[2].text)
            frames.icon3.visible = true
            frames.label3.visible = true
        } else {
            frames.icon3.visible = false
            frames.label3.visible = false
        }

        if (labels.length > 0) {
            frames.name.setPoint(
                FRAMEPOINT_BOTTOMLEFT,
                frames.desc.current,
                FRAMEPOINT_TOPLEFT,
                0,
                0.016
            )
        } else {
            frames.name.setPoint(
                FRAMEPOINT_BOTTOMLEFT,
                frames.desc.current,
                FRAMEPOINT_TOPLEFT,
                0,
                0
            )
        }
        let desc = EquipmentQuality.Util.getQualityDescription(item)
        desc += `|n${itemDB.Description}`

        desc += AbilityIncreaseInstence.getItemIncreaseTip(item)

        frames.desc.setText(desc)

        frames.root.visible = true
        frames.desc.clearPoints()
        if (isHover) {
            let x = InputUtil.getMouseSceneX()
            let y = InputUtil.getMouseSceneY()
            if (x > 0.4) {
                frames.desc
                    .setPoint(
                        FRAMEPOINT_TOPRIGHT,
                        DzGetGameUI(),
                        FRAMEPOINT_BOTTOMLEFT,
                        x - 0.025,
                        y
                    )
                    .setSize(0.14, 0)
            } else {
                frames.desc
                    .setPoint(
                        FRAMEPOINT_TOPLEFT,
                        DzGetGameUI(),
                        FRAMEPOINT_BOTTOMLEFT,
                        x + 0.025,
                        y
                    )
                    .setSize(0.14, 0)
            }
        } else {
            frames.desc
                .setAbsPoint(FRAMEPOINT_BOTTOMRIGHT, 0.79, 0.17)
                .setSize(0.2, 0)
        }
        frames.desc.visible = true
    }

    static hideTooltip(this: void) {
        if (UICustomItemTipView._sl_tooltipFrames == null) {
            return
        }
        UICustomItemTipView._sl_tooltipFrames.root.visible = false
        UICustomItemTipView._sl_tooltipFrames.desc.visible = false

        let tooltip = DzFrameGetTooltip()
        DzFrameClearAllPoints(tooltip)
        DzFrameSetAbsolutePoint(tooltip, FramePoint.bottomRight, 0.8, 0.16)
        UICustomItemTipView.isInventory = false
    }
    static _sl_tooltipFrames: {
        root: Frame
        name: Frame
        desc: Frame
        icon1: Frame
        label1: Frame
        icon2: Frame
        label2: Frame
        icon3: Frame
        label3: Frame
    }
    private static _sl_initTooltip() {
        //只初始化一次
        if (UICustomItemTipView._sl_tooltipFrames != null) {
            return
        }
        const gap = 0.008
        let root = new Frame('BACKDROP', null, null, '_sl_border_backdrop', 0)
        let desc = Frame.createTEXT()
        desc.setAbsPoint(FRAMEPOINT_BOTTOMRIGHT, 0.79, 0.17)
            .setSize(0.2, 0)
            .setFont(0.01113, 'fonts\\fonts.ttf')
        let name = Frame.createTEXT(root.current)
        name.setPoint(
            FRAMEPOINT_BOTTOMLEFT,
            desc.current,
            FRAMEPOINT_TOPLEFT,
            0,
            0
        )
            .setSize(0.2, 0)
            .setFont(0.01113, 'fonts\\fonts.ttf')
        // root.setPoints(desc.current, 0.007, 0.007);
        let icon1 = Frame.createBackDrop(root.current)
            .setSize(0.009, 0.012)
            .setPoint(
                FRAMEPOINT_TOPLEFT,
                name.current,
                FRAMEPOINT_TOPLEFT,
                0,
                -0.013
            )
        let label1 = Frame.createTEXT(root.current)
            .setFont(0.01113, 'fonts\\fonts.ttf')
            .setPoint(FRAMEPOINT_LEFT, icon1.current, FRAMEPOINT_RIGHT, 0.002)
        let icon2 = Frame.createBackDrop(label1.current)
            .setSize(0.009, 0.012)
            .setPoint(FRAMEPOINT_LEFT, label1.current, FRAMEPOINT_RIGHT, 0.002)
        let label2 = Frame.createTEXT(root.current)
            .setFont(0.01113, 'fonts\\fonts.ttf')
            .setPoint(FRAMEPOINT_LEFT, icon2.current, FRAMEPOINT_RIGHT, 0.002)
        let icon3 = Frame.createBackDrop(label2.current)
            .setSize(0.009, 0.012)
            .setPoint(FRAMEPOINT_LEFT, label2.current, FRAMEPOINT_RIGHT, 0.002)
        let label3 = Frame.createTEXT(root.current)
            .setFont(0.01113, 'fonts\\fonts.ttf')
            .setPoint(FRAMEPOINT_LEFT, icon3.current, FRAMEPOINT_RIGHT, 0.002)

        DzFrameSetPoint(
            root.current,
            FRAMEPOINT_TOPLEFT,
            name.current,
            FRAMEPOINT_TOPLEFT,
            -gap,
            gap
        )
        DzFrameSetPoint(
            root.current,
            FRAMEPOINT_BOTTOMRIGHT,
            desc.current,
            FRAMEPOINT_BOTTOMRIGHT,
            gap,
            -gap
        )

        desc.setVisible(false)
        root.setVisible(false)

        UICustomItemTipView._sl_tooltipFrames = {
            root,
            name,
            desc,
            icon1,
            icon2,
            icon3,
            label1,
            label2,
            label3,
        }
    }
    static getCostLabelInfo(item: item): { icon: string; text: string }[] {
        let itemDB = _g_objs.item[GetItemTypeId(item)]
        if (!itemDB) {
            return []
        }
        let labelInfo: { icon: string; text: string }[] = []
        let skid = itemDB.abilList
        if (skid && skid.length > 0) {
            let skDb = _g_objs.ability[skid]
            let manaCost = S2I(skDb.Cost1)
            if (manaCost && manaCost > 0) {
                labelInfo.push({
                    icon: 'UI\\Widgets\\ToolTips\\Human\\ToolTipManaIcon.blp',
                    text: I2S(manaCost),
                })
            }
        }
        //label 1
        let goldCost = S2I(itemDB.goldcost)
        if (goldCost && goldCost > 0) {
            labelInfo.push({
                icon: 'UI\\Feedback\\Resources\\ResourceGold.blp',
                text: I2S(goldCost),
            })
        }
        //label 2
        let lumberCost = S2I(itemDB.lumbercost)
        if (lumberCost && lumberCost > 0) {
            labelInfo.push({
                icon: 'UI\\Feedback\\Resources\\ResourceLumber.blp',
                text: I2S(lumberCost),
            })
        }
        return labelInfo
    }
}
